<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录账号" prop="loginAccount">
        <el-input v-model="queryParams.loginAccount" placeholder="请输入登录账号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['information:information:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['information:information:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['information:information:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['information:information:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="登录账号" align="center" prop="loginAccount" />
      <el-table-column label="统一社会信用代码" width="200" align="center" prop="idNumber" />
      <el-table-column label="服务城市" align="center" prop="city" />
      <el-table-column label="接单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus == 0" type="success">接单中</el-tag>
          <el-tag v-if="scope.row.orderStatus == 1" type="danger">休息中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" prop="accountStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.accountStatus == 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.accountStatus == 1" type="danger">冻结</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="认证时间" align="center" prop="certificationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.certificationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="freezeCompany(scope.row)"
            v-hasPermi="['information:information:edit']">{{ scope.row.accountStatus === 0 ? "冻结" : "解冻" }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleView(scope.row)"
            v-hasPermi="['information:information:remove']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改企业信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="登录账号" prop="loginAccount">
          <el-input v-model="form.loginAccount" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="legalPersonIdCardNo">
          <el-input v-model="form.legalPersonIdCardNo" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <el-input v-model="form.businessLicense" placeholder="请输入营业执照" />
        </el-form-item>
        <el-form-item label="服务城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入服务城市" />
        </el-form-item>
        <el-form-item label="认证时间" prop="certificationTime">
          <el-date-picker clearable v-model="form.certificationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择认证时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 冻结原因对话框 -->
    <!-- <el-dialog title="冻结原因" :visible.sync="dialogVisible" width="30%">
      <el-alert title="账号冻结后将无法进行登录、分账、接单等操作，冻结前请先确认是否有未完成订单。" type="warning" show-icon>
      </el-alert>
      <el-form :model="freezeForm" label-width="120px">
        <el-form-item label="冻结原因:" required>
          <el-input type="textarea" placeholder="请输入冻结原因" v-model="freezeForm.reason" :maxlength="140" show-word-limit>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitFreezeForm">确定</el-button>
      </span>
    </el-dialog>
 -->
  </div>
</template>

<script>
import { addInformation, delInformation, getInformation, listInformation, updateInformation } from "@/api/information/information";
export default {
  name: "Information",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息管理表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        loginAccount: null,
        orderStatus: null,
        accountStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        loginAccount: [
          { required: true, message: "登录账号不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "接单状态不能为空", trigger: "change" }
        ],
        accountStatus: [
          { required: true, message: "账号状态不能为空", trigger: "change" }
        ],
      },
      // 冻结对话框显示控制
      // dialogVisible: false,
      // 冻结表单
      // freezeForm: {
      //   reason: ''
      // },
      // 当前冻结的企业信息
      // currentCompany: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业信息管理列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        loginAccount: null,
        idNumber: null,
        legalPersonName: null,
        legalPersonIdCardNo: null,
        businessLicense: null,
        city: null,
        orderStatus: null,
        accountStatus: null,
        certificationStatus: null,
        certificationTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业信息管理编号为"' + ids + '"的数据项？').then(function () {
        return delInformation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/information/export', {
        ...this.queryParams
      }, `information_${new Date().getTime()}.xlsx`)
    },

    freezeCompany(company) {
      // 只有当账号状态为正常（未冻结）时，才显示提示
      if (company.accountStatus === 0) {
        this.$modal.confirm(
          '账号冻结后将无法进行登录、分账、接单等操作，冻结前请先确认是否有未完成订单。',
          '确认冻结',
          {
            confirmButtonText: '确定冻结',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          // 用户确认冻结
          company.accountStatus = 1;
          updateInformation(company).then(response => {
            this.$modal.msgSuccess("冻结成功");
            this.getList();
          });
        }).catch(() => {
          // 用户取消操作
          this.$modal.msgInfo("冻结操作已取消");
        });
      } else {
        // 直接执行解冻操作
        company.accountStatus = 0;
        updateInformation(company).then(response => {
          this.$modal.msgSuccess("解冻成功");
          this.getList();
        });
      }
    },
    /* 查看企业信息功能，通过路由跳转到详情页面look.vue */
    handleView(row) {
      const id = row.id || this.ids;
      // 导航到详情页面，并将企业ID作为参数传递
      this.$router.push({ name: 'Look', params: { id } });
    }


  }
};
</script>
